<template>
  <div class="site-wrapper site-page--login">
    <div class="site-content__wrapper">
      <div class="site-content">
        <div class="brand-info">
          <h2 class="brand-info__text">IM管理平台</h2>
          <p class="brand-info__intro">IM管理平台基于 vue、element-ui 构建开发，实现后台管理功能，提供一套更优的解决方案。</p>
        </div>
        <div class="login-main">
          <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">

            <el-tab-pane label="企业微信扫码登录" name="ewm">
              <div class="login-main-account">
                <div class="login-img" id="login-img"></div>
              </div>
            </el-tab-pane>

            <el-tab-pane label="账号登录" name="account">
              <!--              <div class="login-title">账号登录</div>-->
              <div class="login-main-account">
                <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" status-icon>
                  <el-form-item prop="userName">
                    <el-input v-model="dataForm.userName" placeholder="帐号"></el-input>
                  </el-form-item>
                  <el-form-item prop="password">
                    <el-input v-model="dataForm.password" type="password" placeholder="密码"></el-input>
                  </el-form-item>
                  <el-form-item prop="captcha">
                    <el-row :gutter="20">
                      <el-col :span="14">
                        <el-input v-model="dataForm.captcha" placeholder="验证码">
                        </el-input>
                      </el-col>
                      <el-col :span="10" class="login-captcha">
                        <img :src="captchaPath" @click="getCaptcha()" alt="">
                      </el-col>
                    </el-row>
                  </el-form-item>
                  <el-form-item>
                    <el-button class="login-btn-submit" type="primary" @click="dataFormSubmit()">登录</el-button>
                  </el-form-item>
                </el-form>
              </div>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { getUUID } from '@/utils'
  export default {
    data () {
      return {
        activeName: 'ewm',
        dataForm: {
          userName: '',
          password: '',
          uuid: '',
          captcha: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '帐号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          captcha: [
            { required: true, message: '验证码不能为空', trigger: 'blur' }
          ]
        },
        captchaPath: ''
      }
    },
    created () {
      this.getCaptcha()
      this.getWeChat()
    },
    methods: {
      getWeChat () {
        // 动态引入企业微信js文件
        const s = document.createElement('script')
        s.type = 'text/javascript'
        s.src = 'http://wwcdn.weixin.qq.com/node/wework/wwopen/js/wwLogin-1.2.4.js'
        const wxElement = document.body.appendChild(s)
        // 调用企业微信二维码方法
        wxElement.onload = function () {
          // eslint-disable-next-line no-undef,no-new
          new WwLogin({
            id: 'login-img',
            appid: 'wwd64ee44fb6bfe7bf',
            agentid: '1000003',
            redirect_uri: encodeURIComponent('http://im-server.natapp1.cc/#/login'),
            state: '123456789'
            // "href": "",
            // "lang": "zh",
          })
        }
      },
      // 提交表单
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/sys/login'),
              method: 'post',
              data: this.$http.adornData({
                'username': this.dataForm.userName,
                'password': this.dataForm.password,
                'uuid': this.dataForm.uuid,
                'captcha': this.dataForm.captcha
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$cookie.set('token', data.token)
                this.$router.replace({ name: 'home' })
              } else {
                this.getCaptcha()
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 获取验证码
      getCaptcha () {
        this.dataForm.uuid = getUUID()
        this.captchaPath = this.$http.adornUrl(`/captcha.jpg?uuid=${this.dataForm.uuid}`)
      },
      // 查询登录状态
      getLoginUserInfo (code) {
        console.log(' code = ' + code)
        this.$http({
          url: this.$http.adornUrl('/sys/login/wx'),
          method: 'post',
          data: this.$http.adornData({
            'code': code
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$cookie.set('token', data.token)
            this.$router.replace({ name: 'home' })
          } else {
            this.getCaptcha()
            this.$message.error(data.msg)
          }
        })
      },
      // tab 切换
      handleClick (tab, event) {
        console.log(tab, event)
      }
    },
    watch: {
      // 企业微信登录后，监听路由的变化
      $route: {
        handler (to) {
          console.log('to' + to.query.code)
          if (to.query.code) {
            this.getLoginUserInfo(to.query.code)
          }
        },
        deep: true
      }
    }
  }
</script>

<style lang="scss">
  .el-tabs--border-card>.el-tabs__header{
    background: #FFFFFF;
  }
  .site-wrapper.site-page--login {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: rgba(38, 50, 56, .6);
    overflow: hidden;
    &:before {
      position: fixed;
      top: 0;
      left: 0;
      z-index: -1;
      width: 100%;
      height: 100%;
      content: "";
      background-image: url(~@/assets/img/login_bg.jpg);
      background-size: cover;
    }
    .site-content__wrapper {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      padding: 0;
      margin: 0;
      overflow-x: hidden;
      overflow-y: auto;
      background-color: transparent;
    }
    .site-content {
      min-height: 100%;
      padding: 30px 500px 30px 30px;
    }
    .brand-info {
      margin: 220px 100px 0 90px;
      color: #fff;
    }
    .brand-info__text {
      margin:  0 0 22px 0;
      font-size: 48px;
      font-weight: 400;
      text-transform : uppercase;
    }
    .brand-info__intro {
      margin: 10px 0;
      font-size: 16px;
      line-height: 1.58;
      opacity: .6;
    }
    .login-main {
      position: absolute;
      top: 0;
      right: 0;
      padding: 150px 20px 180px;
      width: 470px;
      min-height: 100%;
      background-color: #fff;
    }
    .login-main-account{
      min-height: 320px;
      padding: 30px;
      border-radius: 5px;
    }

    .login-title {
      height: 24px;
      line-height: 24px;
      margin: 0 auto;
      color: #0082ef;
      font-size: 20px;
      text-align: center;
    }
    .login-img{
      width: 100%;
      margin: 0 auto;
      padding: 20px;
    }
    .login-captcha {
      overflow: hidden;
      > img {
        width: 100%;
        cursor: pointer;
      }
    }
    .login-btn-submit {
      width: 100%;
      margin-top: 38px;
    }
  }
</style>
